package com.woniuxy.joylifeshop.service.serviceImpl;

import com.woniuxy.common.entity.ShopChatlist;
import com.woniuxy.common.entity.ShopChatlistExample;
import com.woniuxy.common.mapper.ShopChatlistMapper;
import com.woniuxy.joylifeshop.exception.ShopChatException;
import com.woniuxy.joylifeshop.exception.ShopListException;
import com.woniuxy.joylifeshop.service.ChatListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class ChatListServiceIpml implements ChatListService {

    @Autowired
    ShopChatlistMapper shopChatlistMapper;

    /**
    * @Author SooYoo
    * @Description
    * @Param
    * @Return
    **/
    @Override
    public List<ShopChatlist> readChatListByUserId(String userId) {


        try {
            ShopChatlistExample shopChatlistExample = new ShopChatlistExample();
            ShopChatlistExample.Criteria criteria = shopChatlistExample.createCriteria();
            criteria.andUserIdEqualTo(userId);

            List<ShopChatlist> shopChatlists = shopChatlistMapper.selectByExample(shopChatlistExample);


            return shopChatlists;
        } catch (Exception e) {
            e.printStackTrace();
            throw new ShopListException();
        }

    }

    /**
    * @Author SooYoo
    * @Description
    * @Param
    * @Return
    **/
    @Override
    public ShopChatlist ReadChatListByUserIdAndMerchantId(String userId, String merchantId) {

        try {
            ShopChatlistExample shopChatlistExample = new ShopChatlistExample();
            ShopChatlistExample.Criteria criteria = shopChatlistExample.createCriteria();
            criteria.andUserIdEqualTo(userId);
            criteria.andMerchatIdEqualTo(merchantId);

            List<ShopChatlist> shopChatlists = shopChatlistMapper.selectByExample(shopChatlistExample);

            return shopChatlists.get(0);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ShopListException();
        }
    }
    /**
    * @Author SooYoo
    * @Description 删除一条聊天目
    * @Param
    * @Return
    **/
    @Override
    public void deleteOneList(String listId) {

        try {
            shopChatlistMapper.deleteByPrimaryKey(listId);
        } catch (Exception e) {
            e.printStackTrace();

            throw new ShopChatException();
        }


    }
    /**
    * @Author SooYoo
    * @Description
    * @Param
    * @Return
    **/
    @Override
    public String createChatList(String merchantId, String userId) {


        //先验证空
        ShopChatlistExample shopChatlistExample = new ShopChatlistExample();
        ShopChatlistExample.Criteria criteria = shopChatlistExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andMerchatIdEqualTo(merchantId);
        List<ShopChatlist> shopChatlists = shopChatlistMapper.selectByExample(shopChatlistExample);



        if (shopChatlists.isEmpty()){
            ShopChatlist shopChatlist = new ShopChatlist();
            shopChatlist.setListId(UUID.randomUUID().toString());
            shopChatlist.setMerchatId(merchantId);
            shopChatlist.setUserId(userId);
            shopChatlist.setDate(new Date());

            shopChatlistMapper.insert(shopChatlist);

            return "创建聊天成功";
        }

        return "跳转聊天成功";

    }
}
